package me.zhengjie.tool;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 封装树形结构类
 * @author: feixiang
 * @create: 2021-01-27 11:27
 */
public class SelectTreeUtil {

    public static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
    public List<SelectTreeVo> menuCommon;
    public List<Map<String, Object>> list = new ArrayList<>();

    /**
     * @description 生存树形结构列表
     * @param menu 上下级对应关系列表
     * @param value 最上级过滤条件
     * @author feixiang
     * @date 2021/1/28 9:51
    No such property: _1 for class: Script1
     * @return
     */
    public List<Map<String, Object>> menuList(List<SelectTreeVo> menu, String value, boolean needCurrentNode){
        this.menuCommon = menu;
        for (SelectTreeVo selectTreeVo : menu) {
            Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
            String compareId = selectTreeVo.getpId();
            if (ValidateUtil.isEmpty(value)){
                value = "0";
            }else {
                if (needCurrentNode) {
                    compareId = selectTreeVo.getId();
                }
            }
            if(ValidateUtil.areEqual(value ,compareId)){
                mapArr.put("value", selectTreeVo.getId());
                mapArr.put("label", selectTreeVo.getName());
                mapArr.put("children", menuChild(selectTreeVo.getId()));
                list.add(mapArr);
            }
        }
        return list;
    }

    public List<Map<String, Object>> menuChild(String id){
        List<Map<String, Object>> lists = new ArrayList<>();
        for(SelectTreeVo selectTreeVo:menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            String pId = selectTreeVo.getpId();
            if(ValidateUtil.areEqual(pId, id)){
                childArray.put("value", selectTreeVo.getId());
                childArray.put("label", selectTreeVo.getName());
//                childArray.put("pid", selectTreeVo.getpId());
                childArray.put("children", menuChild(selectTreeVo.getId()));
                lists.add(childArray);
            }
        }
        return lists;
    }
}
